// layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
layui.use(['jquery', 'laydate', 'febs', 'form', 'formSelects', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-post'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#post-table-form'),
        tableIns;


    form.verify(validate);
    form.render();

    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'postTable',
            url: ctx + 'post/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {type: 'numbers', title: '序号'},
                {field: 'tenantName', title: '公司名称', minWidth: 280  ,hide:!hasHeadCompanyRole,
                    templet:function (d){
                        var tenant = tenantList.filter(item=>item.tenantId == d.tenantId);
                        return tenant[0].tenantName;
                    }},
                {field: 'postCode', title: '岗位编号', width: 120,align: 'center'},
                {field: 'postName', title: '岗位名称', minWidth: 180},
                {field: 'sort', title: '排序', width: 60,align: 'center'},
                {field: 'status', title: '状态', align:'center',toolbar: '#status-bar' , width: 120  },
                {title: '操作', toolbar: '#post-option',align:'center', width: 160}
            ]],
            toolbar: '#post-toolbar',
            defaultToolbar: []
        });

    }
    function getQueryParams() {
        return {
            postName: $searchForm.find('input[name="postName"]').val(),
            tenantId: $searchForm.find('select[name="tenantId"]').val(),
            postCode: $searchForm.find('input[name="postCode"]').val(),
            status:$searchForm.find('select[name="status"]').val(),
            invalidate_ie_cache: new Date()
        };
    }


    table.on('sort(postTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    febs.reloadPage = function(curr) {
        var _curr = $(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        tableIns.reload({where: getQueryParams(), page: {curr: 1}});
    });

    table.on('toolbar(postTable)', function (obj) {
        febs.modal.open('新增岗位', 'system/post/add', {
            btn: ['提交','取消'],
            area: [$(window).width() <= 750 ? '700px' : '900px','550px'],
            yes: function (index, layero) {
                $('#post-edit').find('#submit').trigger('click');
            },
            btn2: function () {
                layer.closeAll();
            },
            data:{
                tenantId: $searchForm.find('select[name="tenantId"]').val(),
                tenantName: $searchForm.find('select[name="tenantId"]').find('option:selected').text().trim()
            }
        });
        $query.trigger('click');
    });



    table.on('tool(postTable)', function (obj) {
        //表格编辑删除事件
        var postId = obj.data.postId;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            febs.modal.open('修改', 'system/post/update/' + postId, {
                btn: ['提交', '取消'],
                area: [$(window).width() <= 750 ? '700px' : '900px','550px'],
                yes: function (index, layero) {
                    $('#post-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
            });
        } else if (layEvent === 'del') {
            febs.modal.confirm('删除岗位', '确定删除所选岗位？', function () {
                deletePosts(postId);
            });
        }
    });

    function deletePosts(postId) {
        febs.get(ctx + 'post/delete/' + postId, null, function () {
            febs.alert.success('删除岗位成功');
            $query.trigger('click');
        })
    }


    function updatePostStatus(postId,status) {
        //更新岗位状态
        let model={
            postId : postId,
            status : status
        }
        febs.post(ctx + 'post/updatePostStatus', model, function () {
            febs.alert.success('修改岗位状态成功');
            $query.trigger('click');
        })
    }


    form.on('switch(postStatus)', function (obj) {
        //岗位状态控制点击事件
        var checked = obj.elem.checked;
        var status;
        if (checked) {
            status = "1";
        } else {
            status = "0";
        }
        //获取该控件所在行
        var index = obj.othis.parents('tr').eq(0).data('index');
        //获取表中所有数据 按行取数据及其 postId
        var postId =  table.cache['postTable'][index].postId;

        febs.modal.confirm('更改岗位状态', '确认更改岗位状态',function (){
            updatePostStatus(postId,status);
        },function (){
            obj.elem.checked = !checked;
            layer.closeAll();
            form.render('checkbox');
        });

    });

    function getTableHeight() {
        return commonJS.calcTableHeight(10);
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

});